package com.example.bootcurriculumproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bootcurriculumproject.entity.Cart;
import com.example.bootcurriculumproject.entity.Goods;
import com.example.bootcurriculumproject.entity.Orders;
import com.example.bootcurriculumproject.mapper.CartMapper;
import com.example.bootcurriculumproject.mapper.GoodsMapper;
import com.example.bootcurriculumproject.mapper.OrderMapper;
import com.example.bootcurriculumproject.service.CartService;
import com.example.bootcurriculumproject.util.NumUtil;
import com.example.bootcurriculumproject.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisUtil redisUtil;
     //添加商品进购物车
    @Transactional
    public boolean cartAdd(Cart cart){
        String goodId = cart.getGoodsno();
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getGoodsno,goodId);
        Cart cartFind = this.getOne(queryWrapper);
        //查询当前商品是否已经存在到购物车里
        if(cartFind!=null){
            int num = cartFind.getBuynumber();
            log.info("num::::"+num);
            cartFind.setBuynumber(num+1);
            log.info("update buyNumber::::"+cartFind.getBuynumber());
            cartMapper.update(cartFind,queryWrapper);
        }else{
            cart.setCartnum(NumUtil.generateValidateCode(3));
            log.info("nowUid::::"+redisUtil.get("nowUid"));
            cart.setUid((String) redisUtil.get("nowUid"));
            this.save(cart);
        }
        return true;
    }

    //删除一项购物车商品
    @Override
    public boolean cartRemove(String cartNum) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getCartnum,cartNum);
        this.remove(queryWrapper);
        return true;
    }

    //查询购物车商品信息
    @Override
    public List selectCart(String uid) {
        //等user写完保存UID的方法之后，加上UID条件查询
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUid,uid);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public boolean submit(List<Cart> cartList) {
        for(Cart cart : cartList){
            //提交订单
            Orders order = new Orders();
             order.setAddressnum(cart.getAddressnum());
             order.setGoodno(cart.getGoodsno());
             order.setStates(0);
             order.setBuynumber(cart.getBuynumber());
             order.setNowuid((String) redisUtil.get("nowUid"));
             orderMapper.insert(order);
             //删除购物车信息
             this.cartRemove(cart.getCartnum());
             //修改商品存储量
             LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
             goodsLambdaQueryWrapper.eq(Goods::getId,cart.getGoodsno());
            Goods goods = goodsMapper.selectOne(goodsLambdaQueryWrapper);
            goods.setAmount(goods.getAmount()-cart.getBuynumber());
            goodsMapper.update(goods,goodsLambdaQueryWrapper);
        }
        return true;
    }
}
